package com.wofish.web.controller.member;

import com.wofish.common.annotation.Log;
import com.wofish.common.core.controller.BaseController;
import com.wofish.common.core.domain.AjaxResult;
import com.wofish.common.core.page.TableDataInfo;
import com.wofish.common.enums.BusinessType;
import com.wofish.common.utils.IpUtils;
import com.wofish.common.utils.poi.ExcelUtil;
import com.wofish.framework.util.ShiroUtils;
import com.wofish.member.enums.AdvanceTypeEnum;
import com.wofish.member.domain.Member;
import com.wofish.member.enums.PointTypeEnum;
import com.wofish.member.service.IMemberLvService;
import com.wofish.member.service.IMemberService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 会员 信息操作处理
 * 
 * @author wofish
 * @date 2019-04-21
 */
@Controller
@RequestMapping("/member/member")
public class MemberController extends BaseController {
    private String prefix = "member/member";
	
	@Autowired
	private IMemberService memberService;
	@Autowired
	private IMemberLvService memberLvService;
	
	@RequiresPermissions("member:member:view")
	@GetMapping()
	public String member(Model model) {

		Map<Long, String> memberLvNames = memberLvService.getEnableLvName();
		model.addAttribute("memberLvNames", memberLvNames);
	    return prefix + "/member";
	}
	
	/**
	 * 查询会员列表
	 */
	@RequiresPermissions("member:member:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Member member) {
		startPage();
        List<Member> list = memberService.selectMemberList(member);
		return getDataTable(list);
	}
	
	/**
	 * 导出会员列表
	 */
	@RequiresPermissions("member:member:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Member member) {
    	List<Member> list = memberService.selectMemberList(member);
        ExcelUtil<Member> util = new ExcelUtil<Member>(Member.class);
        return util.exportExcel(list, "member");
    }
	
	/**
	 * 新增会员
	 */
	@GetMapping("/add")
	public String add() {
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存会员
	 */
	@RequiresPermissions("member:member:add")
	@Log(title = "会员", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Member member, HttpServletRequest request) {
		member.setRegIp(IpUtils.getIpAddr(request));
		member.setCreateBy(ShiroUtils.getLoginName());
		member.setUpdateBy(ShiroUtils.getLoginName());
		return toAjax(memberService.insertMember(member));
	}

	/**
	 * 修改会员
	 */
	@GetMapping("/edit/{memberId}")
	public String edit(@PathVariable("memberId") Long memberId, ModelMap mmap) {
		Member member = memberService.selectMemberById(memberId);
		mmap.put("member", member);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存会员
	 */
	@RequiresPermissions("member:member:edit")
	@Log(title = "会员", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Member member) {
		member.setUpdateBy(ShiroUtils.getLoginName());
		return toAjax(memberService.updateMember(member));
	}
	
	/**
	 * 删除会员
	 */
	@RequiresPermissions("member:member:remove")
	@Log(title = "会员", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(memberService.deleteMemberByIds(ids));
	}

    /**
	 * 新增会员积分
	 */
	@GetMapping("/addPoint")
	public String addPoint() {
	    return prefix + "/addPoint";
	}

	@RequiresPermissions("member:member:addMemberPoint")
	@Log(title = "添加会员积分", businessType = BusinessType.UPDATE)
	@PostMapping( "/addMemberPoint")
	@ResponseBody
	public AjaxResult addMemberPoint(Long memberId, Integer newVal, String reason) {
	    return toAjax(memberService.addMemberPoint(memberId, newVal, reason, PointTypeEnum.admin));
	}

	/**
	 * 新增会员余额
	 */
	@GetMapping("/addAdvance")
	public String addAdvance() {
	    return prefix + "/addAdvance";
	}

    @RequiresPermissions("member:member:addMemberAdvance")
	@Log(title = "添加会员余额", businessType = BusinessType.UPDATE)
	@PostMapping( "/addMemberAdvance")
	@ResponseBody
	public AjaxResult addMemberAdvance(Long memberId, BigDecimal newVal, String reason) {
	    return toAjax(memberService.addMemberAdvance(memberId, newVal, reason, AdvanceTypeEnum.admin));
	}
	
}
